package com.google.code.lightsomy.output;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.lightsomy.errors.ApplicationException;
import com.google.code.lightsomy.http.HttpResponseBuffer;

/**
 * Renderer to render default JSP.
 * 
 * @author Viktoras Agejevas
 *
 */
public class JspRenderer extends LightsomyRenderer {

	JspRenderer(final HttpServletRequest request, final HttpServletResponse response) {
		super(request, response, Type.JSP);
	}

	@Override
	protected String process(String page) {
		HttpServletResponse response = new HttpResponseBuffer(getResponse());
		RequestDispatcher dispatcher = getRequest().getRequestDispatcher(page);
		
		try {
			dispatcher.include(getRequest(), response);
			return response.toString();
		} catch (Exception e) {
			throw new ApplicationException("Failed to render a page", e);
		}
	}

	@Override
	public Object get(String key) {
		return getRequest().getAttribute(key);
	}

	@Override
	public void set(String key, Object value) {
		getRequest().setAttribute(key, value);
	}
}
